package gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;


import javax.swing.JPanel;
import javax.swing.JScrollPane;

import javax.swing.JTable;
import javax.swing.JTextField;


public class SearchPanel2 extends JPanel implements ActionListener{
	
	private static final long serialVersionUID = 1L;

	private JTextField searchTextField;
	private JTable table;
	private JScrollPane scrollPane;
	private Vector<Vector<Object>> data;
	
	private GUI guiObject;

	public SearchPanel2(GUI guiObject, JPanel callingPanel)
	{
		this.guiObject = guiObject;
		setLayout(new GridBagLayout());
		
		createTable();
		setMinimumSize(new Dimension(300, 200));
	}
	
	private void createTable()
	{
        Vector<String> columnNames = new Vector<String>();
        columnNames.add("Book Name");
        columnNames.add("Author");
        columnNames.add("Edition");
        columnNames.add("Quality");
        columnNames.add("ISBN");
        columnNames.add("Price");

        data = new Vector<Vector<Object>>();
                
        data.addAll(guiObject.search(""));

        table = new JTable(data, columnNames) 
        {
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;   //Disallow the editing of any cell
              }
        };
        
        //SIZE OF THE TABLE IS HARDCODED HERE
        //////////////////////////////////////
        table.setPreferredScrollableViewportSize(new Dimension(300, 150));
        
        table.setFillsViewportHeight(true);
        //table.setGridColor(new Color(255,255,255));
        table.setShowGrid(false);
        
        	table.setColumnSelectionAllowed(false);
        	table.setRowSelectionAllowed(false);
        	//searchTextField.setText("Instance");
     

        //Create the scroll pane and add the table to it.
        scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        
        Constraints.setConstraints(0,3,2,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);
        
        add(scrollPane, Constraints.gbc);
    }

	public void actionPerformed(ActionEvent e)
	{
		if("search".equalsIgnoreCase(e.getActionCommand()))
		{
			//data.add(guiObject.search(searchTextField.getText()));
	        
			Vector<Vector<Object>> holder = guiObject.search(searchTextField.getText());
			
			data.clear();
			data.addAll(holder);
			
	        table.updateUI();
		}
	}

}
